package net.lab1024.sa.admin.module.business.category.domain.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.Data;

import java.util.List;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class RetroResult2 {

    private List<Node> nodes;
    private List<Edge> edges;

    @Override
    public String toString() {
        try {
            ObjectMapper mapper = new ObjectMapper();
            // 启用美化输出和空值处理
            mapper.enable(SerializationFeature.INDENT_OUTPUT);
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

            // 构建自定义序列化器
            return mapper.writerWithDefaultPrettyPrinter()
                    .writeValueAsString(this);
        } catch (JsonProcessingException e) {
            // 确保在序列化失败时返回符合格式的空对象
            return "{\n  \"nodes\": [],\n  \"edges\": []\n}";
        }
    }

    @Data
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class Node {
        private String id;
        private NodeData data;

        // 不序列化 ports 和 dataText 字段
        @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
        private List<String> ports;

        @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
        private String dataText;

        @Data
        @JsonInclude(JsonInclude.Include.NON_NULL)
        public static class NodeData {
            private int level;

            // 使用小写字段名
            @JsonProperty("smiles")
            //@JsonProperty("SMILES")
            private String smiles;  // 注意：输出为小写 "smiles"

            private String image;

            @JsonProperty("in_stock")
            private boolean inStock;  // 注意：输出为下划线格式 "in_stock"

            @JsonProperty("mw")
            private double MW;  // 注意：输出为小写 "mw"
        }
    }

    @Data
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class Edge {
        private String source;
        private String target;
        private EdgeData data;

        @Data
        @JsonInclude(JsonInclude.Include.NON_NULL)
        public static class EdgeData {
            private double score;
            private String template;

            @JsonProperty("template_id")
            private int templateId;  // 注意：输出为下划线格式 "template_id"

            private Object condition; // 可以是数组、字符串或null
            private String text;
        }
    }
}